/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using Whitemice.ZOGI;
using Gtk;

namespace Whitemice.ZOGI.GtkWidgets
{
  [System.ComponentModel.ToolboxItem(true)]
	public class TaskNodeView : CompactNodeView
	{
	
		public EntityEventHandler OnTaskEdit;
		public EntityEventHandler OnTaskAccept;
		public EntityEventHandler OnTaskComment;
		public EntityEventHandler OnTaskReject;
		public EntityEventHandler OnTaskDone;
		public EntityEventHandler OnTaskArchive;
		public EntityEventHandler OnTaskActivate;

		public TaskNodeView() 
			: base(null, false, new Gtk.NodeStore(typeof(TaskListNode)))
		{ SetupColumns(); }
		
		public TaskNodeView(Label status, bool popup) 
			: base(status, popup, new Gtk.NodeStore(typeof(TaskListNode)))
		{
			SetupColumns();
			if (popupEnabled)
				ButtonPressEvent += new ButtonPressEventHandler(OnButonPressEvent);			
		}
		
		protected virtual void SetupColumns()
		{
			AppendTextColumn("Id", 0, true, false, true);
			AppendTextColumn("Name", 1, true, true, true);
			AppendTextColumn("Project", 9, true, true, true);
			AppendTextColumn("Start", 2, true, false, true);
			AppendTextColumn("Creator", 3, true, true, true);
			AppendTextColumn("Status", 4, true, false, true);
			AppendTextColumn("Priority", 5, true, false, true);
			AppendTextColumn("End", 6, true, false, true);
			AppendTextColumn("Executant", 7, true, true, true);
			AppendTextColumn("Completed", 8, true, false, true);
			NodeStore.Clear();		
		}
		
		public void Clear()
		{
			this.NodeStore.Clear();
		}
		
		public override void Fill(IEnumerable<Entity> data, string message)
		{
			this.NodeStore.Clear();
			foreach(Task task in data)
			  if (task != null)
				  this.NodeStore.AddNode(new TaskListNode(task));
			if ((message != null) && (statusLabel != null))
				statusLabel.Text = message;
		}
				
		[GLib.ConnectBefore]		
		public void OnButonPressEvent(object sender, ButtonPressEventArgs args)
		{
			ITreeNode				node;
			PopUpMenuItem 	menuItem;
			ArrayList				actions;
			Task						task;

			if(!(PopupEnabled))
			{
				return;
		  }
			node = ((NodeView)sender).NodeSelection.SelectedNode;
			if (node is TaskListNode)
			{
				task = ((TaskListNode)node).Task;
			}	else 
			  {
			    return;
			  }
			if (task.IsComplete)
				return;
			if (args.Event.Button == 3) 
			{
				actions = ZOGIUtility.AvailableTaskActions(task); 
				Menu popupMenu = new Menu ();
				menuItem = new PopUpMenuItem (task, "Edit");
				menuItem.Activated += new EventHandler(TaskEdit);
				popupMenu.Append (menuItem);
				popupMenu.Append (new SeparatorMenuItem ());
				menuItem = new PopUpMenuItem (task, "Accept");
				menuItem.Activated += new EventHandler(TaskAccept);
				if(actions.Contains(TaskAction.Accept))
					menuItem.Sensitive = true;
				 else
				 		menuItem.Sensitive = false;
				popupMenu.Append (menuItem);
				menuItem = new PopUpMenuItem (task, "Comment");
				menuItem.Activated += new EventHandler(TaskComment);
				if(actions.Contains(TaskAction.Comment))
					menuItem.Sensitive = true;
				 else
				 		menuItem.Sensitive = false;			
				popupMenu.Append (menuItem);			
				menuItem = new PopUpMenuItem (task, "Reject");
				menuItem.Activated += new EventHandler(TaskReject);
				if(actions.Contains(TaskAction.Reject))
					menuItem.Sensitive = true;
				 else
				 		menuItem.Sensitive = false;
				popupMenu.Append (menuItem);
				menuItem = new PopUpMenuItem (task, "Done");
				menuItem.Activated += new EventHandler(TaskDone);
				if(actions.Contains(TaskAction.Done))
					menuItem.Sensitive = true;
				 else
				 		menuItem.Sensitive = false;
				popupMenu.Append (menuItem);
				menuItem = new PopUpMenuItem(task, "Archive");
				menuItem.Activated += new EventHandler(TaskArchive);
				if(actions.Contains(TaskAction.Archive))
					menuItem.Sensitive = true;
				 else
			 		menuItem.Sensitive = false;
				popupMenu.Append (menuItem);
				menuItem = new PopUpMenuItem(task, "Activate");
				menuItem.Activated += new EventHandler(TaskActivate);
				if(actions.Contains(TaskAction.Activate))
					menuItem.Sensitive = true;
				 else
				 		menuItem.Sensitive = false;
				popupMenu.Append (menuItem);						
				popupMenu.Append (menuItem);
				popupMenu.Append (new SeparatorMenuItem ());

				popupMenu.Popup (null, null, null, 3, Gtk.Global.CurrentEventTime);
				popupMenu.ShowAll();							
			} /* End is button #3 */
		} /* End OnButtonPressEvent */
		
		protected void TaskEdit(object _sender, EventArgs _args)
		{ 
			OnTaskEdit(
				(_sender as PopUpMenuItem).ContextObject,
				"edit",
				null); 
		}
		
		protected void TaskAccept(object _sender, EventArgs _args)
		{
			OnTaskAccept(
				(_sender as PopUpMenuItem).ContextObject,
				"accept",
				null); 
		}
		
		protected void TaskComment(object _sender, EventArgs _args)
		{
			OnTaskComment(
				(_sender as PopUpMenuItem).ContextObject,
				"comment",
				null); 
		}
		
		protected void TaskReject(object _sender, EventArgs _args)
		{
			OnTaskReject(
				(_sender as PopUpMenuItem).ContextObject,
				"reject",
				null); 
		}
		
		protected void TaskDone(object _sender, EventArgs _args)
		{ 
			OnTaskDone(
				(_sender as PopUpMenuItem).ContextObject,
				"done",
				null); 
		}
		
		protected void TaskArchive(object _sender, EventArgs _args)
		{
			OnTaskArchive(
				(_sender as PopUpMenuItem).ContextObject,
				"archive",
				null); 
		}
		
		public void TaskActivate(object _sender, EventArgs _args)
		{
			OnTaskActivate(
				(_sender as PopUpMenuItem).ContextObject,
				"activate",
				null); 
		}
		
		public override void Remove(Entity _entity)
		{
		  throw new Exception("Objects cannot be removed from a TaskNodeView.");
		}		
	}
}
